global root_path "/Volumes/Workgroup/Lab/Lab-heimb/Montenovo/Volunteering Paper/Data"
global otheritem_input "$root_path/03_ready_for_TAXSIM/taxsim_input/first_dollar"
global otheritem_output "$root_path/04_TAXSIM_output_firstdollar"


cd "$root_path/03_ready_for_TAXSIM"

capture log close
log using taxsim35_2003, replace

use fam2003er-small.dta, clear

** Generate taxsim ID
gen taxsimid = _n
gen individual_id=_n 

*** Tax year => year
g year = 2002

*** State code => state
** Converts state codes from GSA to SOI for injection into Taxsim.
** PSID state codes come from the documentation to the 1981 extract.
g state = 0
replace state = 1 if state_residence == 01 /* Alabama */
replace state = 2 if state_residence == 50 /* Alaska */
replace state = 3 if state_residence == 02 /* Arizona */
replace state = 4 if state_residence == 03 /* Arkansas */
replace state = 5 if state_residence == 04 /* California */
replace state = 6 if state_residence == 05 /* Colorado */
replace state = 7 if state_residence == 06 /* Connecticut */
replace state = 8 if state_residence == 07 /* Delaware */
replace state = 9 if state_residence == 8 /* DC */
replace state = 10 if state_residence == 09 /* Florida */
replace state = 11 if state_residence == 10 /* Georgia */
replace state = 12 if state_residence == 51 /* Hawaii */
replace state = 13 if state_residence == 11 /* Idaho */
replace state = 14 if state_residence == 12 /* Illinois */
replace state = 15 if state_residence == 13 /* Indiana */
replace state = 16 if state_residence == 14 /* Iowa */
replace state = 17 if state_residence == 15 /* Kansas */
replace state = 18 if state_residence == 16 /* Kentucky */
replace state = 19 if state_residence == 17 /* Louisiana */
replace state = 20 if state_residence == 18 /* Maine */
replace state = 21 if state_residence == 19 /* Maryland */
replace state = 22 if state_residence == 20 /* Massachusetts */
replace state = 23 if state_residence == 21 /* Michigan */
replace state = 24 if state_residence == 22 /* Minnesota */
replace state = 25 if state_residence == 23 /* Mississippi */
replace state = 26 if state_residence == 24 /* Missouri */
replace state = 27 if state_residence == 25 /* Montana */
replace state = 28 if state_residence == 26 /* Nebraska */
replace state = 29 if state_residence == 27 /* Nevada */
replace state = 30 if state_residence == 28 /* NewHampshire */
replace state = 31 if state_residence == 29 /* NewJersey */
replace state = 32 if state_residence == 30 /* NewMexico */
replace state = 33 if state_residence == 31 /* NewYork */
replace state = 34 if state_residence == 32 /* NorthCarolina */
replace state = 35 if state_residence == 33 /* NorthDakota */
replace state = 36 if state_residence == 34 /* Ohio */
replace state = 37 if state_residence == 35 /* Oklahoma */
replace state = 38 if state_residence == 36 /* Oregon */
replace state = 39 if state_residence == 37 /* Pennsylvania */
replace state = 40 if state_residence == 38 /* RhodeIsland */
replace state = 41 if state_residence == 39 /* SouthCarolina */
replace state = 42 if state_residence == 40 /* SouthDakota */
replace state = 43 if state_residence == 41 /* Tennessee */
replace state = 44 if state_residence == 42 /* Texas */
replace state = 45 if state_residence == 43 /* Utah */
replace state = 46 if state_residence == 44 /* Vermont */
replace state = 47 if state_residence == 45 /* Virginia */
replace state = 48 if state_residence == 46 /* Washington */
replace state = 49 if state_residence == 47 /* WestVirginia */
replace state = 50 if state_residence == 48 /* Wisconsin */
replace state = 51 if state_residence == 49 /* Wyoming */

// marital status is married if marital_status_head==1. if single, set marital_status_head==2 
replace marital_status_head=2 if marital_status_head!=1

*** Marital status => marital
// in PSID, marital_status_head=1 if married
// in Taxsim, marital=1 if single
* Making single if never married, widowed, divorced, separated, or other.
generate marital = .
replace marital=1 if marital_status_head==2
replace marital=2 if marital_status_head==1 
// rename to name wanted by taxsim
rename marital mstat

*** Dependents => dependents
g dependents = children
// rename to name wanted by taxsim
rename dependents depx

// For now, we assume all children are younger than 13
// Zwick wrote: Number of dependents under 17 (for tax credit). Need to match individual details file with tax unit file and compute correct age during tax year.
g dep13 = children
g dep17 = children
g dep18 = children

// ages of taxpayers
rename age_head page
rename age_wife sage
*assert age_wife == 0 if marital == 1, otherwise taxsim will return an error
replace sage=0 if mstat == 1

////////////////////////////////////////////////////////////////////

* Dividend income => dividends_taxable
g dividend_inc_head_gen = cond(dividend_inc_head < 999997, dividend_inc_head, 0)
replace dividend_inc_head_gen = 52*dividend_inc_head_gen if dividend_inc_head_unit == 3
replace dividend_inc_head_gen = 26*dividend_inc_head_gen if dividend_inc_head_unit == 4
replace dividend_inc_head_gen = 12*dividend_inc_head_gen if dividend_inc_head_unit == 5
g dividend_inc_wife_gen = cond(dividend_inc_wife < 999997, dividend_inc_wife, 0)
replace dividend_inc_wife_gen = 52*dividend_inc_wife_gen if dividend_inc_wife_unit == 3
replace dividend_inc_wife_gen = 26*dividend_inc_wife_gen if dividend_inc_wife_unit == 4
replace dividend_inc_wife_gen = 12*dividend_inc_wife_gen if dividend_inc_wife_unit == 5

g dividends_taxable = dividend_inc_head_gen + dividend_inc_wife_gen
// rename to name wanted by taxsim
rename dividends_taxable dividends

** Limited generated variables for 2003 and prior.
* Rental income.
g rent_inc_head_gen = cond(rent_inc_head < 999997, rent_inc_head, 0)
replace rent_inc_head_gen = 52*rent_inc_head_gen if rent_inc_head_unit == 3
replace rent_inc_head_gen = 26*rent_inc_head_gen if rent_inc_head_unit == 4
replace rent_inc_head_gen = 12*rent_inc_head_gen if rent_inc_head_unit == 5
g rent_inc_wife_gen = cond(rent_inc_wife < 999997, rent_inc_wife, 0)
replace rent_inc_wife_gen = 52*rent_inc_wife_gen if rent_inc_wife_unit == 3
replace rent_inc_wife_gen = 26*rent_inc_wife_gen if rent_inc_wife_unit == 4
replace rent_inc_wife_gen = 12*rent_inc_wife_gen if rent_inc_wife_unit == 5

* Alimony.
g alimony_inc_head_gen = cond(alimony_inc_head < 99997, alimony_inc_head, 0)
replace alimony_inc_head_gen = 52*alimony_inc_head_gen if alimony_inc_head_unit == 3
replace alimony_inc_head_gen = 26*alimony_inc_head_gen if alimony_inc_head_unit == 4
replace alimony_inc_head_gen = 12*alimony_inc_head_gen if alimony_inc_head_unit == 5

* In 2007, labor and asset income for farms and gardening was combined.
g asset_income = rent_inc_head_gen + ///
                 farm_inc_head_gen + ///
                 unincorp_assetpt_inc_head_gen + ///
                 gardening_inc_head_gen + ///
                 alimony_inc_head_gen + ///
                 rent_inc_wife_gen + unincorp_assetpt_inc_wife_gen

*** Labor income for taxpayer => salary_head
*** Labor income for spouse => salary_spouse
* PSID labor_inc_head_gen (ER40921) Includes:
*   -> wages and salaries (ER40903),
*   -> any separate reports of bonuses (ER40905), 
*   -> overtime (ER40907), 
*   -> tips (ER40909),
*   -> commissions (ER40911), 
*   -> professional practice or trade (ER40913),
*   -> market gardening (ER40915),
*   -> additional job income (ER40917),
*   -> and miscellaneous labor income (ER40919).       
* Methodology follows Butrica and Burkhauser (1997).
// labor income
g head_labor_income = taxable_inc_hh_gen - labor_inc_wife_gen - ///
                      dividend_inc_head_gen - dividend_inc_wife_gen /// 
					  - asset_income
g head_labor_income2 = labor_inc_head_gen
g salary_head = head_labor_income

// rename to name wanted by taxsim
rename salary_head pwages
rename labor_inc_wife_gen swages

// check that swage=0 for non joint returns
count if swages>0 & mstat==1
*drop if wages of spouse is greater than 0, otherwise taxsim will return an error
replace swages=0 if swages>0 & mstat==1
				 
* If we want to use taxsim27, we have to separate interest received from the rest. Below I create the variable for interect received.
* Interest income.
g interest_inc_head_gen = cond(interest_inc_head < 999997, interest_inc_head, 0)
replace interest_inc_head_gen = 52*interest_inc_head_gen if interest_inc_head_unit == 3
replace interest_inc_head_gen = 26*interest_inc_head_gen if interest_inc_head_unit == 4
replace interest_inc_head_gen = 12*interest_inc_head_gen if interest_inc_head_unit == 5
g interest_inc_wife_gen = cond(interest_inc_wife < 999997, interest_inc_wife, 0)
replace interest_inc_wife_gen = 52*interest_inc_wife_gen if interest_inc_wife_unit == 3
replace interest_inc_wife_gen = 26*interest_inc_wife_gen if interest_inc_wife_unit == 4
replace interest_inc_wife_gen = 12*interest_inc_wife_gen if interest_inc_wife_unit == 5
gen interest_received=interest_inc_head_gen + interest_inc_wife_gen
// rename to name wanted by taxsim
rename interest_received intrec

***Short term capital gains or losses. => short_capgains
//Assumption: short term capital gains equal to zero
g short_capgains = 0

***Long term capital gains or losses.
* Capital gains don't seem to be available except in supplementary files, which
* would probably be useful for verification. There may be a way to back this
* out of questions about assets. => long_capgains
//Assumption: long term capital gains equal to zero
g long_capgains = 0

// rename to name wanted by taxsim
rename short_capgains stcg
rename long_capgains ltcg

*** Other property income, excluding interest and alimony (taxsim 27), and including rent, fellowships non-qualified dividends and other income or loss not otherwise 
*** enumerated here. Adjustments such as Keogh and IRA can be entered here as 
*** negative income.(+/-) => other_income
g alimony_paid = cond(alimony_given < 9999997, alimony_given, 0)
g other_income = asset_income - alimony_paid

// rename to name wanted by taxsim
rename other_income otherprop
rename alimony_paid nonprop
				 
*** Taxable pension income. => pensions
* Retirement stuff.
g nonva_retire_inc_head_gen = cond(nonva_retire_inc_head < 999997, nonva_retire_inc_head, 0)
replace nonva_retire_inc_head_gen = 52*nonva_retire_inc_head_gen if nonva_retire_inc_head_unit == 3
replace nonva_retire_inc_head_gen = 26*nonva_retire_inc_head_gen if nonva_retire_inc_head_unit == 4
replace nonva_retire_inc_head_gen = 12*nonva_retire_inc_head_gen if nonva_retire_inc_head_unit == 5
g retire_annuities_inc_wife_gen = cond(retire_annuities_inc_wife < 99997, retire_annuities_inc_wife, 0)
replace retire_annuities_inc_wife_gen = 52*retire_annuities_inc_wife_gen if retire_annuities_inc_wife_unit == 3
replace retire_annuities_inc_wife_gen = 26*retire_annuities_inc_wife_gen if retire_annuities_inc_wife_unit == 4
replace retire_annuities_inc_wife_gen = 12*retire_annuities_inc_wife_gen if retire_annuities_inc_wife_unit == 5

g pensions = nonva_retire_inc_head_gen + retire_annuities_inc_wife_gen

*** Gross Social Security Income => social_security
g social_security = socsec_inc_fu
// rename to name wanted by taxsim
rename social_security gssi

***  Unemployment compensation => unemployment
g unemployment_inc_head_gen = cond(unemployment_inc_head < 999997, unemployment_inc_head, 0)
replace unemployment_inc_head_gen = 52*unemployment_inc_head_gen if unemployment_inc_head_unit == 3
replace unemployment_inc_head_gen = 26*unemployment_inc_head_gen if unemployment_inc_head_unit == 4
replace unemployment_inc_head_gen = 12*unemployment_inc_head_gen if unemployment_inc_head_unit == 5

g unemployment_inc_wife_gen = cond(unemployment_inc_wife < 999997, unemployment_inc_wife, 0)
replace unemployment_inc_wife_gen = 52*unemployment_inc_wife_gen if unemployment_inc_wife_unit == 3
replace unemployment_inc_wife_gen = 26*unemployment_inc_wife_gen if unemployment_inc_wife_unit == 4
replace unemployment_inc_wife_gen = 12*unemployment_inc_wife_gen if unemployment_inc_wife_unit == 5

g unemployment = unemployment_inc_head_gen + unemployment_inc_wife_gen

// rename to name wanted by taxsim
rename unemployment ui

*** Other non-taxable transfer Income such as welfare, municipal 
*** bond interest, and child support that would affect eligibility for state 
*** property tax rebates but would not be taxable at the federal level. => transfers

* SSI income.
g ssi_inc_head_gen = cond(ssi_inc_head < 999997, ssi_inc_head, 0)
replace ssi_inc_head_gen = 52*ssi_inc_head_gen if ssi_inc_head_unit == 3
replace ssi_inc_head_gen = 26*ssi_inc_head_gen if ssi_inc_head_unit == 4
replace ssi_inc_head_gen = 12*ssi_inc_head_gen if ssi_inc_head_unit == 5
g ssi_inc_wife_gen = cond(ssi_inc_wife < 999997, ssi_inc_wife, 0)
replace ssi_inc_wife_gen = 52*ssi_inc_wife_gen if ssi_inc_wife_unit == 3
replace ssi_inc_wife_gen = 26*ssi_inc_wife_gen if ssi_inc_wife_unit == 4
replace ssi_inc_wife_gen = 12*ssi_inc_wife_gen if ssi_inc_wife_unit == 5

g vapension_inc_head_gen = cond(vapension_inc_head < 999997, vapension_inc_head, 0)
replace vapension_inc_head_gen = 52*vapension_inc_head_gen if vapension_inc_head_unit == 3
replace vapension_inc_head_gen = 26*vapension_inc_head_gen if vapension_inc_head_unit == 4
replace vapension_inc_head_gen = 12*vapension_inc_head_gen if vapension_inc_head_unit == 5

g workerscomp_inc_head_gen = cond(workerscomp_inc_head < 999997, workerscomp_inc_head, 0)
replace workerscomp_inc_head_gen = 52*workerscomp_inc_head_gen if workerscomp_inc_head_unit == 3
replace workerscomp_inc_head_gen = 26*workerscomp_inc_head_gen if workerscomp_inc_head_unit == 4
replace workerscomp_inc_head_gen = 12*workerscomp_inc_head_gen if workerscomp_inc_head_unit == 5

g workerscomp_inc_wife_gen = cond(workerscomp_inc_wife < 999997, workerscomp_inc_wife, 0)
replace workerscomp_inc_wife_gen = 52*workerscomp_inc_wife_gen if workerscomp_inc_wife_unit == 3
replace workerscomp_inc_wife_gen = 26*workerscomp_inc_wife_gen if workerscomp_inc_wife_unit == 4
replace workerscomp_inc_wife_gen = 12*workerscomp_inc_wife_gen if workerscomp_inc_wife_unit == 5

g tanf_inc_head_gen = cond(tanf_inc_head < 999997, tanf_inc_head, 0)
replace tanf_inc_head_gen = 52*tanf_inc_head_gen if tanf_inc_head_unit == 3
replace tanf_inc_head_gen = 26*tanf_inc_head_gen if tanf_inc_head_unit == 4
replace tanf_inc_head_gen = 12*tanf_inc_head_gen if tanf_inc_head_unit == 5
g tanf_inc_wife_gen = cond(tanf_inc_wife < 999997, tanf_inc_wife, 0)
replace tanf_inc_wife_gen = 52*tanf_inc_wife_gen if tanf_inc_wife_unit == 3
replace tanf_inc_wife_gen = 26*tanf_inc_wife_gen if tanf_inc_wife_unit == 4
replace tanf_inc_wife_gen = 12*tanf_inc_wife_gen if tanf_inc_wife_unit == 5

g otherwelfare_inc_head_gen = cond(otherwelfare_inc_head < 999997, otherwelfare_inc_head, 0)
replace otherwelfare_inc_head_gen = 52*otherwelfare_inc_head_gen if otherwelfare_inc_head_unit == 3
replace otherwelfare_inc_head_gen = 26*otherwelfare_inc_head_gen if otherwelfare_inc_head_unit == 4
replace otherwelfare_inc_head_gen = 12*otherwelfare_inc_head_gen if otherwelfare_inc_head_unit == 5

g otherwelfare_inc_wife_gen = cond(otherwelfare_inc_wife < 999997, otherwelfare_inc_wife, 0)
replace otherwelfare_inc_wife_gen = 52*otherwelfare_inc_wife_gen if otherwelfare_inc_wife_unit == 3
replace otherwelfare_inc_wife_gen = 26*otherwelfare_inc_wife_gen if otherwelfare_inc_wife_unit == 4
replace otherwelfare_inc_wife_gen = 12*otherwelfare_inc_wife_gen if otherwelfare_inc_wife_unit == 5

g childsupport_inc_head_gen = cond(childsupport_inc_head < 999997, childsupport_inc_head, 0)
replace childsupport_inc_head_gen = 52*childsupport_inc_head_gen if childsupport_inc_head_unit == 3
replace childsupport_inc_head_gen = 26*childsupport_inc_head_gen if childsupport_inc_head_unit == 4
replace childsupport_inc_head_gen = 12*childsupport_inc_head_gen if childsupport_inc_head_unit == 5

g childsupport_inc_wife_gen = cond(childsupport_inc_wife < 999997, childsupport_inc_wife, 0)
replace childsupport_inc_wife_gen = 52*childsupport_inc_wife_gen if childsupport_inc_wife_unit == 3
replace childsupport_inc_wife_gen = 26*childsupport_inc_wife_gen if childsupport_inc_wife_unit == 4
replace childsupport_inc_wife_gen = 12*childsupport_inc_wife_gen if childsupport_inc_wife_unit == 5

g transfers = ssi_inc_head_gen + ssi_inc_wife_gen + ///
              vapension_inc_head_gen + ///
              workerscomp_inc_head_gen + workerscomp_inc_wife_gen + ///
              tanf_inc_head_gen + tanf_inc_wife_gen + ///
              otherwelfare_inc_head_gen + otherwelfare_inc_wife_gen + ///
              childsupport_inc_head_gen + childsupport_inc_wife_gen

*** Rent paid => rent
g rent = cond(rent_amount < 99997 & rent_amount_unit != 9, rent_amount, 0)
replace rent = 52*rent if rent_amount_unit == 3
replace rent = 26*rent if rent_amount_unit == 4
replace rent = 12*rent if rent_amount_unit == 5

// rename to name wanted by taxsim
rename rent rentpaid

*** Property taxes paid. => property_tax
g property_tax = cond(property_tax_annual < 99997, property_tax_annual, 0)

// rename to name wanted by taxsim
rename property_tax proptax

*** Itemized deductions other than mortgage,state income tax and 
*** local property tax. If you have medical expenses remember to only include 
*** the excess over 3%,5% or 7,5% of AGI (for years 60-82, 83-86 and 87+).
*** => itemized_total
* There are itemized fields and then we can also try to compute the value of 
* potential itemizations. Eventually, will try a fancier technique a la Bruce 
* (2000) or Ziliak and Kniesner (2005).
g item_char = 0
g item_med = cond(itemized_medical < 999997, itemized_medical, 0)
g itemized_total = cond(whether_itemized == 1, item_med, 0)

// rename to name wanted by taxsim
rename itemized_total otheritem

*** Child care expenses. => childcare
g childcare = cond(childcare_expense < 999997, childcare_expense, 0)

*** Mortgage interest and other deductions not a preference for the
*** AMT, including charitable contributions, casulty losses in excess of 10% 
*** of AGI and medical expenses between 7.5% and 10% of AGI. => mortgage_interest
*** Ignore second mortgages. Assume all are fixed rates.
g mortgage_principle = cond(mortgage1_principle_remaining < 9999997, mortgage1_principle_remaining, 0)
g mortgage_rate_whole = cond(mortgage1_interest_rate_wholenum < 98, mortgage1_interest_rate_wholenum/100, 0)
g mortgage_rate_frac = cond(mortgage1_interest_rate_fraction < 998, mortgage1_interest_rate_fraction/100000, 0)
g mortgage_rate = mortgage_rate_whole + mortgage_rate_frac
*** First guess, just do 12 months of the same interest.
g mortgage_interest = mortgage_principle*mortgage_rate

g mortgage =  mortgage_interest + item_char 
drop mortgage_interest


** Clean up some messes.
replace page=40 if page==999
replace sage=40 if sage==999
replace pwages=0 if pwages<0

sum mstat depx page sage intrec pwages swages dividends otherprop nonprop gssi rentpaid proptax otheritem ui mortgage stcg ltcg

cd "$otheritem_input/2003"

save taxsim_input_otheritem_fd.dta, replace

** Run taxsim 27

cd "$otheritem_output/2003"

taxsim35, full replace

use taxsim_out, clear

//v18 = Federal Taxable Income
//v36 = State Taxable Income

rename v18 federalTaxable
rename v36 stateTaxable

g fatr = fiitax/federalTaxable
replace fatr = 0 if fatr == .
g satr = siitax/stateTaxable
replace satr = 0 if satr == .
winsor fatr, gen(fatrW) p(.01)
replace fatr = fatrW
winsor satr, gen(satrW) p(.01)
replace satr = satrW

rename fiitax federalTax
rename siitax stateTax

rename taxsimid individual_id
rename v17 deductions_allowed


save taxsim27_2003_otheritem_fd.dta, replace



